package com.qqserver.service;

import java.util.HashMap;
import java.util.Iterator;

/**
 * 该类用于管理和客户端连接的线程
 */
public class ManageServerConnectClientThread {
    //key为用户名,value为相应的线程
    private static HashMap<String,ServerConnectClientThread>hm=new HashMap<>();

    public static HashMap<String, ServerConnectClientThread> getHm() {
        return hm;
    }

    public static void addServerConnectClientThread(String useId, ServerConnectClientThread serverConnectClientThread){
        hm.put(useId,serverConnectClientThread);
    }
    //根据客户端用户名获取相关联的线程
    public static ServerConnectClientThread getServerConnectClientThread(String userId){
        return hm.get(userId);
    }

    //获取在线用户的id,并返回
    public static String getOnlineUser(){
        //集合遍历,遍历key
        Iterator<String> iterator=hm.keySet().iterator();
        String onlineUserList="";
        while(iterator.hasNext()){
            onlineUserList+=iterator.next().toString()+" ";
        }
        return onlineUserList;
    }

    public static void removeServerConnectClientThread(String userId){
        hm.remove(userId);
    }
}
